/**
 * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug
 * Copyright (c) <spug.dev@gmail.com>
 * Released under the AGPL-3.0 License.
 */
import React,{Fragment} from 'react';
import { observer } from 'mobx-react';
import {Table, Button} from 'antd';
import store from "../../pages/generate/ordinary/Store";

@observer
class ActionTable extends React.Component {

    constructor(props) {
        super(props);
        this.state = {}
    }

    // 处理多选数据行
    rowSelection = {
        onChange: (selectedRowKeys, selectedRows) => {
            store.selected_ids = selectedRowKeys;
            console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
        },
    };

    //处理排序、分页
    onChange = (pagination, filters, sorter, extra) => {
        if (sorter.field){
            store.searchMap["sort"] = (sorter.order === "descend" ? "-" : "") + sorter.field;
        }
        store.page = pagination.current;
        store.pageSize = pagination.pageSize;
        store.fetchRecords();
    };


    render() {
        // console.log('tableColumns',store.tableColumns)
        return (
            <Fragment>
                <Table
                    scroll={{ x: "max-content"}}
                    rowSelection={this.rowSelection}
                    onChange={this.onChange}
                    rowKey="id"
                    loading={store.isFetching}
                    dataSource={store.records}
                    pagination={{
                        current:store.page,
                        //是否可以改变 pageSize
                        showSizeChanger: true,
                        showLessItems: true,
                        //只有一页时是否隐藏分页器
                        pageSize:store.pageSize,
                        hideOnSinglePage: true,
                        //指定每页可以显示多少条
                        pageSizeOptions: ['10', '20', '30', '40','50'],
                        showQuickJumper:true,
                        showTotal:(total, range) => {return <Button>{`共${total}条`}</Button>},
                        //数据总数
                        total:store.total,
                    }}
                    columns={store.tableColumns}
                />
            </Fragment>
        )
    }
}

export default ActionTable
